package com.ecube.mqtt.service;

import com.ecube.mqtt.MqttUtils;
import com.ecube.mqtt.mysql.Query;
import org.junit.Test;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class ChargeSendService {

    public Boolean startFail(String env, String externalCode, String chgID, String gunNo, String result, String reson) throws InterruptedException {

        //非常变参数
//        String env = "UAT";
//        String externalCode = "110093";
//        String gunNo = "2";
//        String chgID = "1100930001000001";
        if (gunNo.equals("12")){
            gunNo="1";
        }
        Date date = new Date();
        String index = Query.getChargeSnno(env, chgID);
        String ordersn = Query.getChargeOrderSn(env, chgID);
        System.out.println();
        if (ordersn==null){
            SimpleDateFormat dateFormat2= new SimpleDateFormat("yyyyMMddHHmmss");
            ordersn= dateFormat2.format(date) + getRandom2(6);

        }

        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //启动充电
        Boolean sendStart = MqttUtils.sendStartChargeFailMQTT(env, externalCode, index, chgID, gunNo, result, reson, dateFormat.format(date), ordersn);
        //实时数据
        if(sendStart){
            System.out.println("充电启动成功");
        }else {
            System.out.println("充电启动失败");
        }
        return sendStart;
    }

    public Boolean start(String env, String externalCode, String chgID, String gunNo, String soc) throws InterruptedException {

        //非常变参数
//        String env = "UAT";
//        String externalCode = "110093";
//        String gunNo = "2";
//        String chgID = "1100930001000001";
        String index = Query.getChargeSnno(env, chgID);
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ordersn = Query.getChargeOrderSn(env, chgID);
        if (ordersn==null){
            SimpleDateFormat dateFormat2= new SimpleDateFormat("yyyyMMddHHmmss");
            ordersn= dateFormat2.format(date) + getRandom2(6);

        }
        //启动充电
        Boolean sendStart = MqttUtils.sendStartChargeMQTT(env, externalCode, index, chgID, gunNo, dateFormat.format(date), ordersn);
        if(sendStart){
            System.out.println("充电启动成功");
        }else {
            System.out.println("充电启动失败");
        }
        return sendStart;
    }


    public Boolean auth(String env, String externalCode, String plateNo, String chgID, String gunNo) throws InterruptedException {

        //发送鉴权请求
        Boolean sendAuth = MqttUtils.sendChargeAuthMQTT(env, externalCode, plateNo, chgID, gunNo);

        if(sendAuth){
            System.out.println("鉴权发送成功");
        }else {
            System.out.println("鉴权发送失败");
        }
        return sendAuth;
    }


    public Boolean state(String env, String externalCode, String chgID, String gunNo, String soc) throws InterruptedException {

        //启动充电

        //实时数据
        Boolean sendRealData = false;
        if (gunNo.equals("12")) {
            sendRealData=MqttUtils.send2RealDataMQTT(env, externalCode, chgID, soc);
        }else {
            sendRealData = MqttUtils.sendRealDataMQTT(env, externalCode, chgID, gunNo, soc);
        }
        if(sendRealData){
            System.out.println("实时状态发送成功");
        }else {
            System.out.println("实时状态发送失败");
        }
        return sendRealData;
    }

    public Boolean end(String env, String externalCode, String chgID, String gunNo) throws InterruptedException {
        //常变参数
        String index = Query.getChargeSnno(env, chgID);
        if (gunNo.equals("12")){
            gunNo="1";
        }
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //结束充电
        Boolean sendEndCharge = MqttUtils.sendEndChargeMQTT(env, externalCode, index, chgID, gunNo, dateFormat.format(date));

        if(sendEndCharge){
            System.out.println("充电结束成功");
        }else {
            System.out.println("充电结束失败");
        }
        return sendEndCharge;
    }

    public Boolean chargeResult(String env, String externalCode, String chgID, String gunNo, String plateNo, String time) throws InterruptedException {
        //常变参数
        String index = Query.getChargeSnno(env, chgID);

        //充电事件
        Boolean sendChargeResult =  MqttUtils.sendChargeResultMQTT(env, externalCode, chgID, index, gunNo, plateNo, time);
        if(sendChargeResult){
            System.out.println("充电事件发送成功");
        }else {
            System.out.println("充电事件发送失败");
        }
        return sendChargeResult;
    }

    public Boolean chgState(String env, String stationId, String chgID, String gunno, String state, String batConn) throws InterruptedException {

//        //非常变参数
//        String env = "UAT";
//        String gunNo = "1";
//        String chgID = "1100932023011800";
//        String vin = "Yh821214354323424";
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String orderSn = Query.getOrderSN(env, chgID);
//        String index = Query.getSnno(env, chgID);
        Boolean sendStart = false;
        if (gunno.equals("12")) {
            sendStart=MqttUtils.send2ChgStateMQTT1(env, stationId, chgID, state, batConn, dateFormat.format(date));
        }else {
            sendStart = MqttUtils.sendChgStateMQTT1(env, stationId, chgID, gunno, state, batConn, dateFormat.format(date));
        }


        if(sendStart){
            System.out.println("充电枪状态发送成功");
        }else {
            System.out.println("充电枪状态发送失败");
        }
        return sendStart;
    }


    public static String getRandom2(int len) {
        Random r = new Random();
        StringBuilder rs = new StringBuilder();
        for (int i = 0; i < len; i++) {
            rs.append(r.nextInt(10));
        }
        return rs.toString();
    }

    public static void main(String[] args) {
        //MqttUtils.sendRealDataMQTT("UAT","999081","99908100000017","2","29");
        MqttUtils.sendRealDataMQTT("UAT","999081","99908100000017","1","23");


    }
}
